package vn.sms.lazylove.adapter;

import java.util.ArrayList;
import java.util.List;

import vn.sms.lazylove.R;
import vn.sms.lazylove.UpdateSmsActivity;
import vn.sms.lazylove.database.DataStore;
import vn.sms.lazylove.object.SchedulerSMSItem;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class SMSSchedulerAdapter extends BaseAdapter {

	private Context mContext;
	private Activity mActivity;
	private LayoutInflater mInflater;
	private List<SchedulerSMSItem> mListObject = new ArrayList<SchedulerSMSItem>();
	private List<SchedulerSMSItem> mListDeleted = new ArrayList<SchedulerSMSItem>();
	private boolean mDeleteMode = false;
	private boolean mPlayMode = false;
	DataStore mDataStore;

	public SMSSchedulerAdapter(Activity _activity,
			List<SchedulerSMSItem> _listcontacs) {
		mContext = _activity;
		mActivity = _activity;
		mInflater = LayoutInflater.from(_activity);
		DataStore.init(mContext);
		mDataStore = DataStore.getInstance();
		if (mListObject != null)
			mListObject = _listcontacs;
		if (mListDeleted == null)
			mListDeleted = new ArrayList<SchedulerSMSItem>();
		else
			mListDeleted.clear();
	}

	public void flagClickPlay() {
		mPlayMode = !mPlayMode;
	}

	public List<SchedulerSMSItem> getListDeleted() {
		return mListDeleted;
	}

	public void notifyListObjectChanged(boolean inDeleteMode) {
		mDeleteMode = inDeleteMode;
		notifyDataSetChanged();
	}

	public void setData(List<SchedulerSMSItem> objs) {
		mListObject = objs;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return mListObject.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mListObject.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		Object obj = mListObject.get(position);
		final SchedulerSMSItem smsitem = (SchedulerSMSItem) obj;
		if (convertView == null) {
			convertView = mInflater.inflate(
					R.layout.view_listview_scheduler, null);
			holder = new ViewHolder();		
			holder.setting = (TextView) convertView.findViewById(R.id.txtSetting);
			holder.setting.setSelected(true);
			holder.phone = (TextView) convertView.findViewById(R.id.txtPhone);
			holder.phone.setSelected(true);
			holder.sms = (TextView) convertView.findViewById(R.id.txtSMS);			
			holder.btnDelete = (ImageButton) convertView
					.findViewById(R.id.btn_delete);
			holder.btnPlay = (ImageButton) convertView
					.findViewById(R.id.btn_playing);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (obj instanceof SchedulerSMSItem) {
			holder.setting.setText(smsitem.time + " - " + smsitem.week);
			holder.phone.setText(smsitem.phone);
			holder.sms.setText(smsitem.message);
			holder.btnDelete.setVisibility(ImageButton.GONE);

			if (smsitem.status == 1) {
				holder.btnPlay
						.setBackgroundResource(R.drawable.selector_button_back);
			} else {
				holder.btnPlay
						.setBackgroundResource(R.drawable.selector_button_back);
			}

		}

		if (mDeleteMode) {
			holder.btnDelete.setVisibility(ImageButton.VISIBLE);
			holder.btnDelete
					.setBackgroundResource(R.drawable.selector_button_delete);
			holder.btnDelete.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// remove item
					mListObject.remove(smsitem);
					mListDeleted.add(smsitem);
					notifyDataSetChanged();
				}
			});
		} else {
			holder.btnDelete.setBackgroundResource(R.drawable.ic_arrow_grey);
		}

		holder.btnPlay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				boolean flag = mDataStore.updateStatusScheduler(smsitem);
				if (flag) {
					flagClickPlay();
					if (mPlayMode) {
						holder.btnPlay
								.setBackgroundResource(R.drawable.selector_button_back);
					} else {
						holder.btnPlay
								.setBackgroundResource(R.drawable.selector_button_back);
					}
				}

			}
		});

		convertView.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// click
				Bundle b=new Bundle();
				b.putSerializable("scheduler", smsitem);
				Intent myIn=new Intent(mActivity,UpdateSmsActivity.class);				
				myIn.putExtras(b);
				mActivity.startActivity(myIn);
			}
		});

		return convertView;
	}

	class ViewHolder {
		TextView setting;
		TextView phone;
		TextView sms;
		ImageButton btnDelete;
		ImageButton btnPlay;
	}

}
